/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestString.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package api.lang;

/**
 * java.lang.String测试
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestString {

	/**
	 * indexOf()/lastIndexOf()
	 * 
	 * @param str
	 */
	public static void testFindStr(String str) {
		System.out.println("str.indexOf(\"is\") = " + str.indexOf("is"));
		System.out.println("str.indexOf(\"is\", 4) = " + str.indexOf("is", 4));
		System.out.println("str.lastIndexOf(\"is\") = " + str.lastIndexOf("is"));
		System.out.println("str.lastIndexOf(\"is\", 1) = " + str.lastIndexOf("is", 1));
	}

	/**
	 * substring()
	 * 
	 * @param str
	 */
	public static void testSubStr(String str) {
		System.out.println("str.substring(2) = " + str.substring(2));
		System.out.println("str.substring(2, 9) = " + str.substring(2, 9));
	}

	/**
	 * replace()/replaceFirst()/replaceAll()
	 * 
	 * @param str
	 */
	public static void testReplaceStr(String str) {
		System.out.println("str.replace('i', 'I') = " + str.replace('i', 'I'));
		System.out.println("str.replaceAll(\"is\", \"IS\") = " + str.replaceAll("is", "IS"));
		System.out.println("str.replaceFirst(\"is\", \"IS\") = " + str.replaceFirst("is", "IS"));
	}

	/**
	 * toUpperCase()/toLowerCase()
	 * 
	 * @param str
	 */
	public static void testToUpperOrLower(String str) {
		System.out.println("str.toUpperCase() = " + str.toUpperCase());
		System.out.println("str.toLowerCase() = " + str.toLowerCase());
	}

	/**
	 * charAt()
	 * 
	 * @param str
	 */
	public static void testCharAt(String str) {
		System.out.println("str.charAt(2) = " + str.charAt(2));
		System.out.println(str.toCharArray());
	}

	/**
	 * compare()/compareIgnoreCase()
	 * 
	 * @param str
	 */
	public static void testCompareStr(String str) {
		System.out.println("str.compareTo(\"I am in Beijing\") = " + str.compareTo("I am in Beijing"));
		System.out.println(
				"str.compareToIgnoreCase(\"I am in Beijing\") = " + str.compareToIgnoreCase("I am in Beijing"));
	}

	/**
	 * equals()
	 * 
	 * @param str
	 */
	public static void testEqualsStr(String str) {
		System.out.println("str.equals(\"I am in Beijing\") = " + str.equals("I am in Beijing"));
		System.out.println("str.equalsIgnoreCase(\"I am in Beijing\") = " + str.equalsIgnoreCase("I am in Beijing"));
		System.out.println("str.startsWith(\"Th\") = " + str.startsWith("Th"));
		System.out.println("str.endsWith(\"t!\") = " + str.endsWith("t!"));
	}

	/**
	 * 正则表达式
	 */
	public static void testRegex() {
		String str = "aab        aaa  bb    ab";

		String pattern1 = "^[a-zA-Z| ]*$";
		System.out.println("用正则表达式匹配成功? " + str.matches(pattern1));
		System.out.println(str.replaceAll("\\s{2,}", " "));
		System.out.println(str.replaceFirst("\\s{2,}", " "));

		String[] ss = str.split("\\s{1,}");
		System.out.println("用正则表达式按空格分割:");
		for (int i = 0; i < ss.length; i++) {
			System.out.println(ss[i]);
		}
		System.out.println("用正则表达式按空格分割, 指定最大分割段数为3:");
		ss = str.split("\\s{1,}", 3);
		for (int i = 0; i < ss.length; i++) {
			System.out.println(ss[i]);
		}
	}

	public static void main(String[] args) {
		String str = "This is a String object!";
		System.out.println("str的值: " + str);

		testFindStr(str);
		System.out.println();
		testSubStr(str);
		System.out.println();
		testReplaceStr(str);
		System.out.println();
		testToUpperOrLower(str);
		System.out.println();
		testCharAt(str);
		System.out.println();
		testCompareStr(str);
		System.out.println();
		testEqualsStr(str);
		System.out.println();
		testRegex();
		System.out.println();

		StringPool.createString();
	}
}

/**
 * Java的String pool
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class StringPool {

	/**
	 * 创建字符串
	 */
	public static void createString() {
		/**
		 * String pool方式创建对象<br>
		 * 
		 * 1. 查找String pool中是否存在"abc"这个对象<br>
		 * 2. 如果不存在, 则在String pool中创建, 然后将String pool中的这个"abc"对象的地址返回赋值给str1<br>
		 * 3. 如果存在, 则不创建任何对象, 直接将String pool中的对象地址返回赋值给str2<br>
		 * 
		 */
		String str1 = "abc";// Sting pool中创建一个对象
		String str2 = "abc";// 不创建新对象
		System.out.println("str1 == str2 " + (str1 == str2));// true

		/**
		 * new方式创建对象<br>
		 * 
		 * 1. 在String pool中查找有没有"abc"这个字符串对象<br>
		 * 2. 如果有, 则不在String pool中创建, 直接在Heap中创建一个"abc"对象并将Heap中的地址返回赋值给str3<br>
		 * 3. 如果没有, 则先在String pool中创建一个"abc"对象,
		 * 然后在Heap中创建一个"abc"对象并将Heap中的地址返回赋值给str4<br>
		 */
		String str3 = new String("abc");// Heap中创建一个对象
		String str4 = new String("abc");// Heap中创再建一个对象
		System.out.println("str1 == str3 " + (str1 == str3)); // false
		System.out.println("str3 == str4 " + (str3 == str4)); // false
		System.out.println("str3.equals(str4) " + str3.equals(str4)); // true

		String str5 = "hello";
		String str6 = " world";
		String str7 = str5 + str6;

		/**
		 * String是常量, 其对象一旦创建就无法改变, 当使用 + 拼接字符串时, 会生成新的String对象
		 */
		String str8 = "hello" + " world";
		System.out.println("str7 == str8 " + (str7 == str8)); // false
		System.out.println("str7.equals(str8) " + str7.equals(str8));// true

		String str9 = "hello world";
		System.out.println("str8 == str9 " + (str8 == str9)); // true

		String str10 = new String("hello");
		/**
		 * intern方法<br>
		 * 
		 * 1. 如果String pool中有这个字符串对象, 则直接返回String pool中的对象地址<br>
		 * 2. 如果String pool中没有这个字符串对象, 则在String pool中创建一个新的对象并将地址返回<br>
		 */
		System.out.println("str5 == str10 " + (str5 == str10.intern())); // true
	}
}
